package org.springblade.modules.ehb.wrapper;

import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.ehb.service.IEhbPolicyService;
import org.springblade.modules.ehb.vo.EhbPolicyDrawVO;
import org.springblade.modules.policy.entity.PolicyDrawMaterial;
import org.springblade.modules.policy.service.IPolicyDrawMaterialService;
import org.springblade.modules.system.entity.File;
import org.springblade.modules.system.service.ICityService;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IFileService;

import java.util.ArrayList;
import java.util.List;

public class EhbPolicyDrawWrapper extends BaseEntityWrapper<EhbPolicyDrawVO, EhbPolicyDrawVO> {

	private static IDeptService deptService;
	private static ICityService cityService;
	private static IEhbPolicyService policyService;
	private static IPolicyDrawMaterialService policyDrawMaterialService;
	private static IFileService fileService;

	static {
		deptService = SpringUtil.getBean(IDeptService.class);
		cityService = SpringUtil.getBean(ICityService.class);
		policyService = SpringUtil.getBean(IEhbPolicyService.class);
		policyDrawMaterialService = SpringUtil.getBean(IPolicyDrawMaterialService.class);
		fileService = SpringUtil.getBean(IFileService.class);
	}

	public static EhbPolicyDrawWrapper build() {
		return new EhbPolicyDrawWrapper();
	}

	@Override
	public EhbPolicyDrawVO entityVO(EhbPolicyDrawVO entity) {
		if (Func.isNotEmpty(entity.getEnterpriseNature())) {
			entity.setEnterpriseNatureList(Func.toStrList(",", entity.getEnterpriseNature()));
		} else {
			entity.setEnterpriseNatureList(new ArrayList<>());
		}
		if (Func.isNotEmpty(entity.getEnterpriseScale())) {
			entity.setEnterpriseScaleList(Func.toStrList(",", entity.getEnterpriseScale()));
		} else {
			entity.setEnterpriseScaleList(new ArrayList<>());
		}
		if (Func.isNotEmpty(entity.getEnterpriseSpecial())) {
			entity.setEnterpriseSpecialList(Func.toStrList(",", entity.getEnterpriseSpecial()));
		} else {
			entity.setEnterpriseSpecialList(new ArrayList<>());
		}
		if (Func.isNotEmpty(entity.getIndustryType())) {
			entity.setIndustryTypeList(Func.toStrList(",", entity.getIndustryType()));
		} else {
			entity.setIndustryTypeList(new ArrayList<>());
		}

		entity.setMatMatterPolicyList(policyService.selectRelatedPolicy(entity.getPolicys()));
		List<PolicyDrawMaterial> policyDrawMaterials = policyDrawMaterialService.selectMaterial(entity.getId());
		policyDrawMaterials.forEach(i -> {
			if (Func.isNotBlank(i.getFiles())) {
				String[] fileId = i.getFiles().split(",");
				File byId = fileService.getById(fileId[0]);
				List<File> list = new ArrayList<>(16);
				list.add(byId);
				i.setEmptyLists(list);
			}
		});
		entity.setMatterMaterialList(policyDrawMaterials);
//		entity.setMatterMaterialList(new ArrayList<>());
		entity.setMatterProcessList(new ArrayList<>());

		if (Func.isNotEmpty(entity.getCreateUserDept()) && entity.getCreateUserDept() > -1L) {
			String deptName = deptService.getDeptName(entity.getCreateUserDept());
			entity.setCreateUserDeptName(deptName == null ? "" : deptName);
		}
		if (Func.isNotEmpty(entity.getUpdateUserDept()) && entity.getUpdateUserDept() > -1L) {
			String deptName = deptService.getDeptName(entity.getUpdateUserDept());
			entity.setUpdateUserDeptName(deptName == null ? "" : deptName);
		}
		if (Func.isNotEmpty(entity.getSuitLevel())) {
			String regionName = cityService.getLabelByCode(entity.getSuitLevel());
			entity.setSuitLevelName(regionName == null ? "" : regionName);
		}

		return entity;
	}
}
